package com.web.main.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.entity.Code;
import com.common.entity.ResponseResult;
import com.web.main.entity.form.SupplierForm;
import com.web.main.entity.param.SupplierQuery;
import com.web.main.entity.vo.SupplierVo;
import com.web.main.service.SupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 长顺
 * @since 2020-11-21
 */
@RestController
@RequestMapping("/supplier")
@Api(tags = "供应商接口")
@Validated
@CrossOrigin
public class SupplierController {
    @Autowired
    SupplierService supplierService;

    @ApiOperation(value = "查询一个")
    @GetMapping("/getOne")
    public ResponseResult getOne(@ApiParam(value = "供应商主键", required = true)
                                 @RequestParam("supplierId") String supplierId){
        SupplierVo oneSupplier = supplierService.getOneSupplier(supplierId);
        return ResponseResult.success(Code.REQUEST_SUCCESS).Data("supplier", oneSupplier);
    }

    @ApiOperation(value = "添加供应商")
    @PostMapping("/add")
    public ResponseResult add(@ApiParam(value = "供应商对象")
                              @Validated(SupplierForm.Add.class) SupplierForm supplierParam) {

        supplierService.addSupplier(supplierParam);
        return ResponseResult.success(Code.REQUEST_SUCCESS);
    }


    @ApiOperation(value = "删除供应商")
    @DeleteMapping("/delete")
    public ResponseResult delete(@ApiParam(value = "供应商主键", required = true)
                                 @RequestParam("supplierId") String supplierId) {
        supplierService.removeSupplierById(supplierId);
        return ResponseResult.success(Code.REQUEST_SUCCESS);
    }

    @ApiOperation(value = "修改供应商信息")
    @PutMapping("/modify")
    public ResponseResult modify(
            @ApiParam(name = "supplierParam", value = "供应商实体类")
            @Validated({SupplierForm.Modify.class}) SupplierForm supplierParam) {
        System.out.println("------>" + supplierParam);
        supplierService.modifySupplier(supplierParam);
        return ResponseResult.success(Code.REQUEST_SUCCESS);
    }

    @ApiOperation(value = "分页显示+条件")
    @PostMapping("/page")
    public ResponseResult page( @ApiParam(value = "查询对象")
                                @RequestBody SupplierQuery supplierQuery) {
        Page<SupplierVo> supplierVoPage = supplierService.pageQuery(supplierQuery);
        return ResponseResult.success(Code.REQUEST_SUCCESS).Data("supplierPages", supplierVoPage);
    }

    @ApiOperation(value = "获取供应商个数")
    @GetMapping("/getCount")
    public ResponseResult getCount() {
        int count = supplierService.count(null);
        return ResponseResult.success(Code.REQUEST_SUCCESS).Data("getCount", count);
    }

}

